//
//  HQQAPIGenerator.m
//  HQQNetworkExemple
//
//  Created by 黄强强 on 16/7/8.
//  Copyright © 2016年 黄强强. All rights reserved.
//

#import "HQQAPIGenerator.h"

static NSString * const defaultHost = @"hqqsk8.top/fasttransfer";
static NSString * const defaultHostTest = @"hqqsk8.top/fasttransfer/test";

static HQQAPIGenerator *_instance = nil;

@implementation HQQAPIGenerator

#pragma mark - init

+ (instancetype)sharedGenerator
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

- (NSString *)API:(NSString *)urlString
{
    if (!urlString) {
        return nil;
    }
    
    NSString *protocal = nil;
    NSString *host = nil;
    
    if (self.isTestMode) {
        // 测试统一使用http
        protocal = @"http";
        host = defaultHostTest;
    }
    else{
        protocal = self.defaultProtocol;
        host = defaultHost;
    }
    
    return [self appResultURLWithProtocal:protocal host:host url:urlString];
}

- (NSString *)appResultURLWithProtocal:(NSString *)protocal host:(NSString *)host url:(NSString *)url
{
    NSMutableString *resultURL = [NSMutableString string];
    
    [resultURL appendString:protocal];
    [resultURL appendString:@"://"];
    [resultURL appendString:host];
    [resultURL appendString:@"/"];
    [resultURL appendString:url];
    
    return [resultURL copy];
}

- (NSString *)defaultProtocol
{
    if (!_defaultProtocol) {
        return @"http";
    }
    return _defaultProtocol;
}

@end
